package com.wqh.zeromq.taskRecord;

import org.zeromq.SocketType;
import org.zeromq.ZContext;
import org.zeromq.ZMQ;
import java.util.Random;

/**
 * 将任务Push到分发到多个Worker
 */

public class TaskVentilator {
    public static void main(String[] args) {
        getVentilator();
    }

    private static void getVentilator() {
        try(ZContext context = new ZContext()){
            ZMQ.Socket ventilator = context.createSocket(SocketType.PUSH);
            ventilator.bind("tcp://*:5555");

            System.out.println("Press Enter when the workers are ready: ");
            System.in.read();
            System.out.println("Sending tasks to workers\n");

            //发布100个任务
            int totalTask=0;
            Random random = new Random(System.currentTimeMillis());
            for (int i=0;i<100;i++){
                int workload=random.nextInt(100)+1;
                totalTask+=workload;

                ventilator.send(totalTask+".",0);
            }
            System.out.println(totalTask);
            Thread.sleep(1000);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
